package com.kpmg.datalake.operatelog.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kpmg.datalake.operatelog.dao.OperateLogMapper;
import com.kpmg.datalake.operatelog.dto.OperationLogDTO;
import com.kpmg.datalake.operatelog.model.OperateLog;
import com.kpmg.datalake.operatelog.service.IOperateLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 通用操作日志 服务实现类
 * </p>
 *
 * @author azhao6
 * @since 2018-08-08
 */
@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog>
    implements IOperateLogService {

  /**
   * 获取日志列表
   * @return
   */
  @Override
  public Page<OperateLog> getOperationLogList(OperationLogDTO operationLogDTO) {
    Page<OperateLog> page = new Page<>(operationLogDTO.getCurrent(), operationLogDTO.getSize());

    Wrapper<OperateLog> operateLogEntityWrapper = new EntityWrapper<>();

    if(!StringUtils.isEmpty(operationLogDTO.getLogType())){
      operateLogEntityWrapper.eq("log_type", operationLogDTO.getLogType());
    }

    if(!StringUtils.isEmpty(operationLogDTO.getLogDesc())){
      operateLogEntityWrapper.like("log_desc", operationLogDTO.getLogDesc());
    }

    operateLogEntityWrapper.orderBy("begin_time", false);

    Page<OperateLog> operateLogPage = super.selectPage(page, operateLogEntityWrapper);

    //当前页大于总页数，需要展示所选类型的第一页
    if(operationLogDTO.getCurrent() > operateLogPage.getPages()){
      page = new Page<>(1, operationLogDTO.getSize());
      operateLogPage = super.selectPage(page, operateLogEntityWrapper);
    }

    return operateLogPage;
  }
}
